package com.syqc.monitor.ui.cars

import android.text.Html
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.utils.loadStringImgError
import com.syqc.entity.GpsDetail
import com.syqc.monitor.R
import com.syqc.utils.ToolsUtil.getListBgRes
import com.syqc.utils.ToolsUtil.stateColor
import com.syqc.utils.Units

/**
 *@Author pc_hfjs
 *@Date 2022/3/8 14:50
 *@Desc
 */
class CarsAdapter :
    BaseQuickAdapter<GpsDetail, BaseViewHolder>(R.layout.item_car_info_card) {
    var isPlate: Boolean = true
    override fun convert(holder: BaseViewHolder, item: GpsDetail) {
        holder.getView<LinearLayout>(R.id.llItem)
            .setBackgroundResource(getListBgRes(item.state))

        holder.setText(R.id.tvPlate, if (isPlate) item.carPlate else item.carName)
            .setText(R.id.tvSpeed, "${item.speed}${Units.speed}")
            .setText(R.id.tvRunStop, item.runStopTime)

        val lrTvState = holder.getView<TextView>(R.id.tvStateAcc)
        val string =
            "<font color=${stateColor(item.state)} >${item.stateCn}</font><font color='#666666'> | ${
                if (item.accTime.isEmpty()) context.getString(
                    R.string.desc_acc_close
                ) else {
                    "ACC${item.accTime}"
                }
            }</font>"
        lrTvState.text = Html.fromHtml(string)
        holder.getView<ImageView>(R.id.ivCarIcon).loadStringImgError(context, item.iconLink)
        val text = holder.getView<TextView>(R.id.tvAdrr)
        if (item.addr.isNullOrEmpty()) {
            text.isVisible = false
        } else {
            text.isVisible = true
            text.text = item.addr
        }
    }

}